[[...path]].page.tsx 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. import React, { useMemo } from 'react';
  2. import { IUserHasId } from '@growi/core';
  3. import { model as mongooseModel } from 'mongoose';
  4. import {
  5. GetServerSideProps, GetServerSidePropsContext,
  6. } from 'next';
  7. import { useTranslation } from 'next-i18next';
  8. import { serverSideTranslations } from 'next-i18next/serverSideTranslations';
  9. import dynamic from 'next/dynamic';
  10. import Head from 'next/head';
  11. import { useRouter } from 'next/router';
  12. import { BasicLayout } from '~/components/Layout/BasicLayout';
  13. import { CrowiRequest } from '~/interfaces/crowi-request';
  14. import type { RendererConfig } from '~/interfaces/services/renderer';
  15. import { ISidebarConfig } from '~/interfaces/sidebar-config';
  16. import { IUserUISettings } from '~/interfaces/user-ui-settings';
  17. import { UserUISettingsModel } from '~/server/models/user-ui-settings';
  18. import {
  19. useCurrentUser, useIsSearchPage,
  20. useIsSearchServiceConfigured, useIsSearchServiceReachable,
  21. useCsrfToken, useIsSearchScopeChildrenAsDefault,
  22. useRegistrationWhiteList, useShowPageLimitationXL, useRendererConfig,
  23. } from '~/stores/context';
  24. import {
  25. usePreferDrawerModeByUser, usePreferDrawerModeOnEditByUser, useSidebarCollapsed, useCurrentSidebarContents, useCurrentProductNavWidth,
  26. } from '~/stores/ui';
  27. import loggerFactory from '~/utils/logger';
  28. import { NextPageWithLayout } from '../_app.page';
  29. import {
  30. CommonProps, getNextI18NextConfig, getServerSideCommonProps, generateCustomTitle,
  31. } from '../utils/commons';
  32. const logger = loggerFactory('growi:pages:me');
  33. type Props = CommonProps & {
  34. isSearchServiceConfigured: boolean,
  35. isSearchServiceReachable: boolean,
  36. isSearchScopeChildrenAsDefault: boolean,
  37. userUISettings?: IUserUISettings
  38. sidebarConfig: ISidebarConfig,
  39. rendererConfig: RendererConfig,
  40. showPageLimitationXL: number,
  41. // config
  42. registrationWhiteList: string[],
  43. };
  44. const PersonalSettings = dynamic(() => import('~/components/Me/PersonalSettings'), { ssr: false });
  45. // const MyDraftList = dynamic(() => import('~/components/MyDraftList/MyDraftList'), { ssr: false });
  46. const InAppNotificationPage = dynamic(
  47. () => import('~/components/InAppNotification/InAppNotificationPage').then(mod => mod.InAppNotificationPage), { ssr: false },
  48. );
  49. const MePage: NextPageWithLayout<Props> = (props: Props) => {
  50. const router = useRouter();
  51. const { t } = useTranslation(['translation', 'commons']);
  52. const { path } = router.query;
  53. const pagePathKeys: string[] = Array.isArray(path) ? path : ['personal-settings'];
  54. const mePagesMap = useMemo(() => {
  55. return {
  56. 'personal-settings': {
  57. title: t('User Settings'),
  58. component: <PersonalSettings />,
  59. },
  60. // drafts: {
  61. // title: t('My Drafts'),
  62. // component: <MyDraftList />,
  63. // },
  64. 'all-in-app-notifications': {
  65. title: t('commons:in_app_notification.notification_list'),
  66. component: <InAppNotificationPage />,
  67. },
  68. };
  69. }, [t]);
  70. const getTargetPageToRender = (pagesMap, keys): {title: string, component: JSX.Element} => {
  71. return keys.reduce((pagesMap, key) => {
  72. return pagesMap[key];
  73. }, pagesMap);
  74. };
  75. const targetPage = getTargetPageToRender(mePagesMap, pagePathKeys);
  76. useIsSearchPage(false);
  77. useCurrentUser(props.currentUser ?? null);
  78. useRegistrationWhiteList(props.registrationWhiteList);
  79. useShowPageLimitationXL(props.showPageLimitationXL);
  80. // commons
  81. useCsrfToken(props.csrfToken);
  82. // UserUISettings
  83. usePreferDrawerModeByUser(props.userUISettings?.preferDrawerModeByUser ?? props.sidebarConfig.isSidebarDrawerMode);
  84. usePreferDrawerModeOnEditByUser(props.userUISettings?.preferDrawerModeOnEditByUser);
  85. useSidebarCollapsed(props.userUISettings?.isSidebarCollapsed ?? props.sidebarConfig.isSidebarClosedAtDockMode);
  86. useCurrentSidebarContents(props.userUISettings?.currentSidebarContents);
  87. useCurrentProductNavWidth(props.userUISettings?.currentProductNavWidth);
  88. // page
  89. useIsSearchServiceConfigured(props.isSearchServiceConfigured);
  90. useIsSearchServiceReachable(props.isSearchServiceReachable);
  91. useIsSearchScopeChildrenAsDefault(props.isSearchScopeChildrenAsDefault);
  92. useRendererConfig(props.rendererConfig);
  93. const title = generateCustomTitle(props, 'GROWI');
  94. return (
  95. <>
  96. <Head>
  97. <title>{title}</title>
  98. </Head>
  99. <div className="dynamic-layout-root">
  100. <header className="py-3">
  101. <div className="container-fluid">
  102. <h1 className="title">{ targetPage.title }</h1>
  103. </div>
  104. </header>
  105. <div id="grw-fav-sticky-trigger" className="sticky-top"></div>
  106. <div id="main" className='main'>
  107. <div id="content-main" className="content-main grw-container-convertible">
  108. {targetPage.component}
  109. </div>
  110. </div>
  111. </div>
  112. </>
  113. );
  114. };
  115. MePage.getLayout = function getLayout(page) {
  116. return (
  117. <BasicLayout>{page}</BasicLayout>
  118. );
  119. };
  120. async function injectUserUISettings(context: GetServerSidePropsContext, props: Props): Promise<void> {
  121. const req = context.req as CrowiRequest<IUserHasId & any>;
  122. const { user } = req;
  123. const UserUISettings = mongooseModel('UserUISettings') as UserUISettingsModel;
  124. const userUISettings = user == null ? null : await UserUISettings.findOne({ user: user._id }).exec();
  125. if (userUISettings != null) {
  126. props.userUISettings = userUISettings.toObject();
  127. }
  128. }
  129. async function injectServerConfigurations(context: GetServerSidePropsContext, props: Props): Promise<void> {
  130. const req: CrowiRequest = context.req as CrowiRequest;
  131. const { crowi } = req;
  132. const {
  133. searchService,
  134. configManager,
  135. } = crowi;
  136. props.isSearchServiceConfigured = searchService.isConfigured;
  137. props.isSearchServiceReachable = searchService.isReachable;
  138. props.isSearchScopeChildrenAsDefault = configManager.getConfig('crowi', 'customize:isSearchScopeChildrenAsDefault');
  139. props.registrationWhiteList = configManager.getConfig('crowi', 'security:registrationWhiteList');
  140. props.showPageLimitationXL = crowi.configManager.getConfig('crowi', 'customize:showPageLimitationXL');
  141. props.sidebarConfig = {
  142. isSidebarDrawerMode: configManager.getConfig('crowi', 'customize:isSidebarDrawerMode'),
  143. isSidebarClosedAtDockMode: configManager.getConfig('crowi', 'customize:isSidebarClosedAtDockMode'),
  144. };
  145. props.rendererConfig = {
  146. isEnabledLinebreaks: configManager.getConfig('markdown', 'markdown:isEnabledLinebreaks'),
  147. isEnabledLinebreaksInComments: configManager.getConfig('markdown', 'markdown:isEnabledLinebreaksInComments'),
  148. adminPreferredIndentSize: configManager.getConfig('markdown', 'markdown:adminPreferredIndentSize'),
  149. isIndentSizeForced: configManager.getConfig('markdown', 'markdown:isIndentSizeForced'),
  150. plantumlUri: process.env.PLANTUML_URI ?? null,
  151. blockdiagUri: process.env.BLOCKDIAG_URI ?? null,
  152. // XSS Options
  153. isEnabledXssPrevention: configManager.getConfig('markdown', 'markdown:rehypeSanitize:isEnabledPrevention'),
  154. attrWhiteList: crowi.xssService.getAttrWhiteList(),
  155. tagWhiteList: crowi.xssService.getTagWhiteList(),
  156. highlightJsStyleBorder: crowi.configManager.getConfig('crowi', 'customize:highlightJsStyleBorder'),
  157. };
  158. }
  159. // /**
  160. // * for Server Side Translations
  161. // * @param context
  162. // * @param props
  163. // * @param namespacesRequired
  164. // */
  165. async function injectNextI18NextConfigurations(context: GetServerSidePropsContext, props: Props, namespacesRequired?: string[] | undefined): Promise<void> {
  166. // preload all languages because of language lists in user setting
  167. const nextI18NextConfig = await getNextI18NextConfig(serverSideTranslations, context, namespacesRequired, true);
  168. props._nextI18Next = nextI18NextConfig._nextI18Next;
  169. }
  170. export const getServerSideProps: GetServerSideProps = async(context: GetServerSidePropsContext) => {
  171. const req = context.req as CrowiRequest<IUserHasId & any>;
  172. const { user, crowi } = req;
  173. const result = await getServerSideCommonProps(context);
  174. // check for presence
  175. // see: https://github.com/vercel/next.js/issues/19271#issuecomment-730006862
  176. if (!('props' in result)) {
  177. throw new Error('invalid getSSP result');
  178. }
  179. const props: Props = result.props as Props;
  180. if (user != null) {
  181. const User = crowi.model('User');
  182. const userData = await User.findById(req.user.id).populate({ path: 'imageAttachment', select: 'filePathProxied' });
  183. props.currentUser = userData.toObject();
  184. }
  185. await injectUserUISettings(context, props);
  186. await injectServerConfigurations(context, props);
  187. await injectNextI18NextConfigurations(context, props, ['translation', 'admin', 'commons']);
  188. return {
  189. props,
  190. };
  191. };
  192. export default MePage;